{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78a0a9ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1634\n",
      "8208\n",
      "9474\n"
     ]
    }
   ],
   "source": [
    "# 1. 四位玫瑰数\n",
    "# 四位玫瑰数是4位数的自幂数。自幂数是指一个n位数，它的每个位上的数字的n次幂之和等于它本身\n",
    "\n",
    "def isRose(n): # 判断n是否是四位玫瑰数，是返回True，不是返回False\n",
    "    L = list(str(n))\n",
    "    sum = 0\n",
    "    for i in range(len(L)):\n",
    "        L[i] = int(L[i])\n",
    "        sum += L[i] ** 4\n",
    "    if n == sum:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "    \n",
    "def checkNumber(n): # 检查字符串 n 是不是构成4位数，是返回整数n，不是返回-1\n",
    "    L = list(n)\n",
    "    if len(L) == 4:\n",
    "        return int(n)\n",
    "    else:\n",
    "        return -1\n",
    "\n",
    "def printRose(a, b):#输出区间【a,b】之间所有的四位玫瑰数，不存在输出提示信息\n",
    "    n = 0\n",
    "    for num in range(a, b + 1):\n",
    "        if isRose(num):\n",
    "            print(num)\n",
    "            n = n + 1\n",
    "    if n == 0:\n",
    "        print(\"此区间没有四位玫瑰数\")\n",
    "    \n",
    "def main():\n",
    "    a = checkNumber(input())\n",
    "    if a == -1:\n",
    "        print(\"第一个数不是四位数\")\n",
    "        return\n",
    "    b = checkNumber(input())\n",
    "    if b == -1:\n",
    "        print(\"第二个数不是四位数\")\n",
    "        return\n",
    "    if a <= b:\n",
    "        printRose(a, b)\n",
    "    else:\n",
    "        printRose(b, a)\n",
    "    \n",
    "main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a3c3c97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "没有同构数\n"
     ]
    }
   ],
   "source": [
    "# 2. 判断同购数\n",
    "# 随机输入若干个不超过2位的正整数（输入-1表示输入结束），找出其中所有同构数并排序输出。（正整数n若是它平方数的尾部，则称n为同构数。如5的平方数是25，且5是25的尾部，那么5就是一个同构数。同理，25的平方为625，25也是同构数）\n",
    "# 输入时要求首先判断输入数位数（1-2位）是否正确，判断输入内容是否是数字，然后判断是否是同构数\n",
    "# 输出的同构数要求从小到大排序，结果显示在一行，各同构数间空格分隔\n",
    "'''\n",
    "思路：采用str.endswith()来判断末尾是否相等f\n",
    "'''\n",
    "\n",
    "def isTGS(n): # 输入的n为int\n",
    "        return str(n ** 2).endswith(str(n))\n",
    "\n",
    "def getTGS():\n",
    "        L = []\n",
    "        s = input().strip()\n",
    "        while s != \"-1\":\n",
    "                if 0 < len(s) <= 2 and s.isdigit(): # 判断不超过两位，且内容为数字\n",
    "                        n = int(s)\n",
    "                        if n > 0 and isTGS(n):\n",
    "                                L.append(n)\n",
    "                s = input().strip()\n",
    "        return L\n",
    "\n",
    "def main():\n",
    "        L = getTGS()\n",
    "        if len(L) == 0:\n",
    "            print(\"没有同构数\")\n",
    "        else:\n",
    "            print(\"同构数有：\",end=\"\")\n",
    "            L = [str(i) for i in L]\n",
    "            for x in L:\n",
    "                print(x, end=' ')\n",
    "if __name__ == '__main__':\n",
    "        main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f7149bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 统计字符\n",
    "def gt5(s): # 格式错误：缺少冒号\n",
    "    gt5s = 0\n",
    "    for c in s:\n",
    "        if c >= '5' and c <= '9':\n",
    "            gt5s += 1\n",
    "    return gt5s # 缺少返回值\n",
    "\n",
    "while True:\n",
    "    s = input() # 格式错误\n",
    "    if s.lower() == 'end':\n",
    "        break # 逻辑错误：无法结束\n",
    "    print('字符串为\"{}\"，其中单个数字大于等于5的有{}个'.format(s, gt5(s)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8184f73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "获得初赛资格的成绩\n",
      "第1组:\t150 \n",
      "第2组:\t149 143 \n",
      "第3组:\t\n",
      "第4组:\t152 149 \n"
     ]
    }
   ],
   "source": [
    "# 4. 调高成绩预选\n",
    "# 计算每一个预选赛跳高比赛小组中能进入初赛的成绩，初赛资格线：142\n",
    "groups = (\n",
    "    (78,150,90,102,110,141),        # 第一组\n",
    "    (149,88,132,95,108,112,143),    # 第二组\n",
    "    (100,123,125,99,106,118,133),\n",
    "    (152,86,132,95,70,122,149,124),\n",
    ")\n",
    "\n",
    "def passList(scores, lowlimit):\n",
    "    L = list()\n",
    "    for score in scores:\n",
    "        if score >= lowlimit:\n",
    "            L.append(score)\n",
    "    return L\n",
    "\n",
    "def main():\n",
    "    count = 1\n",
    "    print(\"获得初赛资格的成绩\")\n",
    "    for group in groups:\n",
    "        print(f\"第{count}组:\\t\", end='')\n",
    "        L = passList(group, 142)\n",
    "        for num in L:\n",
    "            print(num, end=' ')\n",
    "        print() # 注意：print函数默认会换一行，所以不需要写print('\\n')\n",
    "        count += 1\n",
    "\n",
    "main()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8815e45d",
   "metadata": {},
   "source": [
    "<font color = 'red'>以下进制转换多复习一下！</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "58f61bf6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "将100转换为16进制的结果为：64\n",
      "将100转换为2进制的结果为：1100100\n"
     ]
    }
   ],
   "source": [
    "# 5. 进制转换\n",
    "def DToR(m, r):\n",
    "    '''\n",
    "    m: 十进制数字\n",
    "    r: 需要转化为的进制\n",
    "    '''\n",
    "    t = \"\" # 创建一个空字符串，用于存储转化后的r进制数\n",
    "    m = int(m)\n",
    "    while m != 0 and r != 0:\n",
    "        c = m % r    # 求模\n",
    "        if (c > 9):\n",
    "            t = chr(c - 10 + 65) + t # 从后往前转化，所以已转化部分应该放在后面\n",
    "        else:\n",
    "            t = str(c) + t\n",
    "        m = m // r   # 整除\n",
    "    return t\n",
    "\n",
    "while True:\n",
    "    ipt = input()\n",
    "    if ipt == 'q':\n",
    "        break\n",
    "    r = int(input())\n",
    "    rst = DToR(ipt, r)\n",
    "    print('将%s转换为%d进制的结果为：%s' %(ipt, r, rst))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82939518",
   "metadata": {},
   "source": [
    "**<font color='blue'>lambda函数</font>**\n",
    "\n",
    "Python 使用 lambda 来创建匿名函数。lambda 函数是一种小型、匿名的、内联函数，它可以具有任意数量的参数，但只能有一个表达式。\n",
    "\n",
    "匿名函数不需要使用 def 关键字定义完整函数。\n",
    "\n",
    "lambda 函数通常用于编写简单的、单行的函数，通常在需要函数作为参数传递的情况下使用，例如在 map()、filter()、reduce() 等函数中。\n",
    "\n",
    "lambda 函数特点：\n",
    "\n",
    "- lambda 函数是匿名的，它们没有函数名称，只能通过赋值给变量或作为参数传递给其他函数来使用。\n",
    "- lambda 函数通常只包含一行代码，这使得它们适用于编写简单的函数。\n",
    "\n",
    "`lambda arguments: expression`\n",
    "\n",
    "- lambda是 Python 的关键字，用于定义 lambda 函数。\n",
    "- arguments 是参数列表，可以包含零个或多个参数，但必须在冒号(:)前指定。\n",
    "- expression 是一个表达式，用于计算并返回函数的结果。\n",
    "\n",
    "```python\n",
    "# 没有参数\n",
    "f = lambda: \"Hello, world!\"\n",
    "print(f())  # 输出: Hello, world!\n",
    "\n",
    "# 有参数\n",
    "x = lambda a : a + 10\n",
    "print(x(5))\n",
    "\n",
    "x = lambda a, b : a * b\n",
    "print(x(5, 6))\n",
    "\n",
    "# lambda 函数通常与内置函数如 map()、filter() 和 reduce() 一起使用，以便在集合上执行操作\n",
    "numbers = [1, 2, 3, 4, 5]\n",
    "squared = list(map(lambda x: x**2, numbers))\n",
    "print(squared)  # 输出: [1, 4, 9, 16, 25]\n",
    "\n",
    "numbers = [1, 2, 3, 4, 5, 6, 7, 8]\n",
    "even_numbers = list(filter(lambda x: x % 2 == 0, numbers))\n",
    "print(even_numbers)  # 输出：[2, 4, 6, 8]\n",
    "\n",
    "from functools import reduce\n",
    " \n",
    "numbers = [1, 2, 3, 4, 5]\n",
    " \n",
    "# 使用 reduce() 和 lambda 函数计算乘积\n",
    "product = reduce(lambda x, y: x * y, numbers)\n",
    "print(product)  # 输出：120\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8efe1d0e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "402.67\n"
     ]
    }
   ],
   "source": [
    "# 6. 计算平均值\n",
    "# 定义lambda函数average(x,y,z)，输入3个整数，调用函数average计算平均值并输出，保留两位小数\n",
    "avg = lambda x, y, z: (x + y + z) / 3\n",
    "x = int(input())\n",
    "y = int(input())\n",
    "z = int(input())\n",
    "print(\"{:.2f}\".format(avg(x, y, z)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2949e00b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "新整数是1\n"
     ]
    }
   ],
   "source": [
    "# 7. 构造整数\n",
    "#定义lambda函数getLastBit返回一个数的个位数\n",
    "getLastBit = lambda num: num % 10\n",
    "\n",
    "#定义一个可变参数的函数makeNumber，求任意个整数的个位数构成的一个新整数\n",
    "def makeNumber(*a):\n",
    "    # print(a)\n",
    "    s = \"\"\n",
    "    for num in a:\n",
    "        s += str(getLastBit(num))\n",
    "    return int(s) # 这里要转化为int，这样对于s=\"000\"的情况，可以转化为s=0\n",
    "\n",
    "#输入一组逗号分隔的数，截取个位数构成的新整数。\n",
    "# t = eval(input())\n",
    "t = map(int, input().split(\",\"))\n",
    "print(\"新整数是{}\".format(makeNumber(*t)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e4b46c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "新整数是456\n"
     ]
    }
   ],
   "source": [
    "#定义lambda函数getLastBit返回一个数的个位数\n",
    "getLastBit = lambda num: num % 10\n",
    "\n",
    "#定义一个可变参数的函数makeNumber，求任意个整数的个位数构成的一个新整数\n",
    "def makeNumber(a, *b):\n",
    "    s = str(getLastBit(a))\n",
    "    for num in b:\n",
    "        s += str(getLastBit(num))\n",
    "    return int(s)\n",
    "\n",
    "#输入一组逗号分隔的数，截取个位数构成的新整数。\n",
    "t = eval(input())\n",
    "print(\"新整数是%d\" % makeNumber(*t)) # 将列表/元组中每个元素作为独立的参数传递\n",
    "# makeNumber(*t)会把t的每个元素分别传给a, *b，而不是把整个t作为一个参数传递\n",
    "# 如果直接写makeNumber(t)，那么a会接收到整个列表或元组，*b为空，导致结果不正确"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
